package repositories

import (
	"log"
	"qimo_work/config"
	"qimo_work/models"

	"gorm.io/gorm"
)

// AddComment 添加评论
func AddComment(comment *models.Comment) error {
	// 启动事务
	tx := config.DB.Begin()

	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
			log.Println("Transaction rolled back due to panic:", r)
		}
	}()

	// 插入评论数据
	if err := tx.Create(comment).Error; err != nil {
		tx.Rollback()
		log.Println("Error creating comment:", err)
		return err
	}

	// 提交事务
	if err := tx.Commit().Error; err != nil {
		log.Println("Error committing transaction:", err)
		return err
	}

	log.Println("Comment added successfully:", comment.ID)
	return nil
}
func GetCommentsByPostID(postID uint, page, pageSize int) ([]models.Comment, int64, error) {
	var comments []models.Comment
	var total int64

	// 获取总评论数
	if err := config.DB.Model(&models.Comment{}).Where("post_id = ?", postID).Count(&total).Error; err != nil {
		log.Println("Error counting comments:", err)
		return nil, 0, err
	}

	// 分页查询
	offset := (page - 1) * pageSize
	if err := config.DB.
		Where("post_id = ?", postID).
		Offset(offset).
		Limit(pageSize).
		Preload("User"). // 加载评论用户
		Preload("Replies", func(db *gorm.DB) *gorm.DB {
			return db.Preload("User") // 递归加载子评论的用户信息
		}).
		Find(&comments).Error; err != nil {
		log.Println("Error fetching comments:", err)
		return nil, 0, err
	}

	return comments, total, nil
}

// GetCommentByID 根据ID获取评论
func GetCommentByID(id uint) (models.Comment, error) {
	var comment models.Comment

	if err := config.DB.
		Preload("User").
		Preload("Replies").
		First(&comment, id).Error; err != nil {
		log.Println("Error fetching comment by ID:", err)
		return comment, err
	}

	return comment, nil
}

// UpdateComment 更新评论
func UpdateComment(comment *models.Comment) error {
	// 启动事务
	tx := config.DB.Begin()

	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
			log.Println("Transaction rolled back due to panic:", r)
		}
	}()

	// 更新评论数据
	if err := tx.Save(comment).Error; err != nil {
		tx.Rollback()
		log.Println("Error updating comment:", err)
		return err
	}

	// 提交事务
	if err := tx.Commit().Error; err != nil {
		log.Println("Error committing transaction:", err)
		return err
	}

	log.Println("Comment updated successfully:", comment.ID)
	return nil
}

// IsPostExists 检查文章 ID 是否存在
func IsPostExists(postID uint) bool {
	var post models.Post
	// 查询文章是否存在
	err := config.DB.First(&post, postID).Error
	// 如果查询成功（无错误），则文章存在
	return err == nil
}

// DeleteComment 删除评论
func DeleteComment(id uint) error {
	// 启动事务
	tx := config.DB.Begin()

	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
			log.Println("Transaction rolled back due to panic:", r)
		}
	}()

	// 删除评论
	if err := tx.Delete(&models.Comment{}, id).Error; err != nil {
		tx.Rollback()
		log.Println("Error deleting comment:", err)
		return err
	}

	// 提交事务
	if err := tx.Commit().Error; err != nil {
		log.Println("Error committing transaction:", err)
		return err
	}

	log.Println("Comment deleted successfully:", id)
	return nil
}
